import 'package:flutter/material.dart';
import 'dart:math';

class PageValuekey extends StatefulWidget {
  static const String routeName = "/PageValuekey";

  final String title;

  const PageValuekey(this.title);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<PageValuekey> {
  List<SquareItem1> list = [
    // const SquareItem1('上上上'),
    // const SquareItem1('中中中'),
    // const SquareItem1('下下下')
    const SquareItem1('上上上', key: ValueKey(111),),
    const SquareItem1('中中中', key: ValueKey(222),),
    const SquareItem1('下下下', key: ValueKey(333),)
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: list,
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            list.removeAt(0);
          });
        },
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

class SquareItem1 extends StatefulWidget {
  final String title;
  const SquareItem1(this.title, {Key? key}) : super(key: key);

  @override
  _SquareItem1State createState() => _SquareItem1State();
}

class _SquareItem1State extends State<SquareItem1> {
  final color = Color.fromRGBO(
      Random().nextInt(256), Random().nextInt(256), Random().nextInt(256), 1.0);
  @override
  Widget build(BuildContext context) {
    return Container(
      width: 100,
      height: 100,
      color: color,
      child: Text(widget.title),
    );
  }
}
